//
//  NavgationBarDemo.m
//  Exercise
//
//  Created by zhouxiaobo on 15/11/15.
//  Copyright © 2015年 mapath. All rights reserved.
//

#import "NavgationBarDemo.h"
#import "NavigationSub2Demo.h"

@implementation NavgationBarDemo

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
   
    //--------------------------------- 上面NavigationBar的设置 ------------------------------//
    //设定左侧按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(leftAction)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    //设定中间
    //self.title = @"测试";
    UIImageView *titleImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"qqlogo"]];
    titleImage.frame = CGRectMake(0, 0, 43, 20);
    titleImage.contentMode = UIViewContentModeScaleAspectFit;
    self.navigationItem.titleView = titleImage;
    
    //设定右边
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    
    //--------------------------------- 下面ToolBar的设置 ------------------------------//
    //1.自定义一个UIView来做为下面的ToolBar
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, screenHeight - 44, screenWidth, 44)];
    toolBar.barStyle = UIBarStyleDefault;
    
    //将自定义的view加上去
    //[self.view addSubview:toolBar];
    
    //设置toolbar里的按钮
    UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];  //使用系统按钮
    
    UIBarButtonItem *textButton = [[UIBarButtonItem alloc]initWithTitle:@"文字" style:UIBarButtonItemStylePlain target:self action:nil];    //使用文字设置按钮
    
    UIBarButtonItem *imageButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"tt_e28"] style:UIBarButtonItemStylePlain target:self action:nil];   //直接使用图片,会变色
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tt_e28"]];
    UIBarButtonItem *imageViewButton = [[UIBarButtonItem alloc]initWithCustomView:imageView]; //使用封装好的imageView,不会变色,但会失去点击效果
    
    //用来撑位置的东西 UIBarButtonSystemItemFlexibleSpace - 撑到最大 UIBarButtonSystemItemFixedSpace - 可以定义宽度
    UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    //UIBarButtonItem *fixBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    //fixBarButton.width = 8;
    
    //将barbutton 加入到自定义的ToolBar里
    NSArray *barArr = @[cameraButton,flexBarButton,textButton,flexBarButton,imageButton,flexBarButton,imageViewButton];
    [toolBar setItems:barArr animated:YES];
    
    //2. 直接使用UINavigation里带的toolbar
    [self.navigationController setToolbarHidden:NO animated:NO];
    [self setToolbarItems:barArr animated:NO];
    
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(screenWidth/2 - 40, 180, 80, 30);
    btn1.backgroundColor = [UIColor redColor];
    [btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [btn1 setTitle:@"Push" forState:UIControlStateNormal];
    [btn1 setTitle:@"Push" forState:UIControlStateHighlighted];
    [btn1 addTarget:self action:@selector(goNext) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    self.navigationController.delegate = self;
    
    if([self respondsToSelector:@selector(someMothod:)]){
    
    }
    
}

- (void)goNext {
    NavigationSub2Demo *navigationSub2Demo = [[NavigationSub2Demo alloc]init];
    navigationSub2Demo.titleNumber++;
    [self.navigationController pushViewController:navigationSub2Demo animated:YES];
}

- (void)leftAction {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"DemoAlert" message:@"This is a demo alert" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)rightAction {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"DemoAlert" message:@"This is a demo alert" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"执行" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了执行");
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSLog(@"即将进入到%@界面",viewController);
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSLog(@"进入到%@界面",viewController);
}

@end
